package com.sch.www.sys.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.common.base.BaseController;
import com.sch.www.common.base.ResultBean;
import com.sch.www.common.validator.NotBlankFluentValidator;
import com.sch.www.common.validator.SizeFluentValidator;
import com.sch.www.sys.orm.entity.manual.Role;
import com.sch.www.sys.services.RoleToResourceServices;
import com.sch.www.sys.services.RoleServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RequestMapping("/v1/role")
@RestController
public class RoleController extends BaseController {

    @Autowired
    RoleServices roleServices;
    @Autowired
    RoleToResourceServices roleToResourceServices;


    @RequestMapping("/listAll")
    public ResultBean<List<Role>> listAll() {
        ResultBean<List<Role>> resultBean = ResultBean.newInstance();
        List<Role> roleList = roleServices.selectAll();
        return resultBean.setData(roleList).setSuccess();
    }


    @RequestMapping("/add")
    public ResultBean add(@RequestBody Role role) {
        ResultBean<List<Role>> resultBean = ResultBean.newInstance();

        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(role.getName(), new NotBlankFluentValidator("name"))
                .on(role.getCode(), new NotBlankFluentValidator("code"))
                .on(role.getActive(), new SizeFluentValidator(0, 1, "active"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        roleServices.insertSelective(role);
        return resultBean.setSuccess();
    }

    @RequestMapping("/modify")
    public ResultBean modify(@RequestBody Role role) {
        ResultBean<List<Role>> resultBean = ResultBean.newInstance();

        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(role.getId(), new NotBlankFluentValidator("id"))
                .on(role.getName(), new NotBlankFluentValidator("name"))
                .on(role.getCode(), new NotBlankFluentValidator("code"))
                .on(role.getActive(), new SizeFluentValidator(0, 1, "active"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        if (roleServices.updateByIdSelective(role)) {
            return resultBean.setSuccess();
        } else {
            return resultBean.setError();
        }
    }

    @RequestMapping("/addResourceForRole")
    public ResultBean addResourceForRole(@RequestBody Map body) {
        String roleId = (String) body.get("roleId");
        List<String> resourceIdList = (List<String>) body.get("resourceIdList");

        ResultBean<List<Role>> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(roleId, new NotBlankFluentValidator("roleId"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        roleToResourceServices.insert(roleId, resourceIdList);
        return resultBean.setSuccess();

    }


}
